import java.util.StringTokenizer;


class P10515_39643 {

	static int[][] values = new int[11][101];
	
	public static void main(String[] args) {
		
		for (int m = 0; m < 10; m++) {
			int n;
			for (n = 0; n < 100; n++) {
				values[m][n] = getLastDigit(m, n);
			}
			values[m][n] = (values[m][n-1] * m) % 10;
		}
		
		while (true) {
			
			StringTokenizer st = new StringTokenizer(readLn());
			
			String m = st.nextToken();
			String n = st.nextToken();
			
			if ("0".equals(m) && "0".equals(n)) break;
			
			process(m, n);
			
		}
		
	}
	
	private static void process(String m, String n) {
		int mInt = m.charAt(m.length()-1) - '0';
		String nSubString;
		if (n.length() > 2) nSubString = n.substring(n.length()-2);
		else nSubString = n;
		int nInt;
		if (nSubString.equals("00")) nInt = 100;
		else nInt = Integer.parseInt(nSubString);
		
		System.out.println(values[mInt][nInt]);
		
		
	}

	private static int getLastDigit(int m, int n) {
		
		int lastDigit = 1;
		
		for (int k = 0; k < n; k++) {
			lastDigit *= m;
			
			lastDigit = lastDigit % 10;
		}
		
		return lastDigit;
		
	}

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString());
	}
	
}
